package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.entity.Photography;
import com.oceanread.cn.entity.Treelist1;
import com.oceanread.cn.entity.Treelist2;
import com.oceanread.cn.service.IPhotographyService;
import com.oceanread.cn.service.ITreelist1Service;
import com.oceanread.cn.service.ITreelist2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/photography")
@CrossOrigin()

public class PhotographyController {

    @Autowired
    private IPhotographyService iPhotographyService;

    /*前端系统*/
    /*获取总表*/

    @GetMapping("/getall/{pId}")
    public JSONArray getall2(@PathVariable("pId") String pId) {
        String key = "pId";
        String value = pId;

        JSONArray jsonArray = new JSONArray();
        List<Photography> list = iPhotographyService.query(key, value);
        for (Photography treelist2 : list) {//遍历查询到的数据
            if (treelist2.getpId().equals(value)) {//找到第一层数据
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(treelist2);
                JSONArray jsonArray_22 = new JSONArray();
                for (Photography treelist_22 : list) {
                    if (treelist_22.getpId().equals(treelist2.getId())) {//找到第二层数据
                        JSONObject jsonObject_22 = (JSONObject) JSONObject.toJSON(treelist_22);
                        jsonArray_22.add(jsonObject_22);
                        jsonObject.put("parts",jsonArray_22);
                    }
                }
                jsonArray.add(jsonObject);
            }
        }
        return jsonArray;
    }

    /*后台系统*/
    @GetMapping("/getList2")
    public JSONObject getList2(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                               @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iPhotographyService.getTablelist(page, size));
        object.put("total", iPhotographyService.getnum());
        return object;
    }

    @PostMapping("/update2")
    public Integer update2(@RequestBody Photography photography) {
        return iPhotographyService.update(photography);
    }
    @PostMapping("/insert2")
    public Integer insert2(@RequestBody Photography photography) {
        return iPhotographyService.insert(photography.getpId(), photography.getId(),
                photography.getName(), photography.getPath(), photography.getType(),
                photography.getTyperoot(), photography.getOpen(),photography.getInfluencechild(),
                photography.getRegion(),photography.getX(),photography.getY(),photography.getCameraHight());
    }
    @GetMapping("/delete2")
    public Integer delete2(@RequestParam(name = "sysid") int sysid) {
        return iPhotographyService.delete(sysid);
    }

    @GetMapping("/getList2query")
    public JSONObject getList2query(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                    @RequestParam(name = "size", required = true, defaultValue = "10") int size,
                                    @RequestParam(name = "key") String key,
                                    @RequestParam(name = "value") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iPhotographyService.getTablelistquery(key, value, page, size));
        object.put("total", iPhotographyService.getnumquery(key, value));
        return object;
    }


}
